import os
import subprocess
from datetime import datetime
from PIL import Image, ImageDraw, ImageFont


def capture_screenshot(err_info):
    # 获取当前时间并格式化为字符串
    current_time = datetime.now().strftime("%Y-%m-%d-%H_%M_%S")

    # 创建目录
    directory = "D:/jb/tu/app/"
    os.makedirs(directory, exist_ok=True)

    # 执行ADB命令进行截图
    subprocess.run(["adb", "shell", "screencap", "-p", "/sdcard/screenshot.png"])
    '_{current_time}'
    # 将截图文件复制到本地目录
    local_path = os.path.join(directory, f"定位失败__{err_info}.png")
    subprocess.run(["adb", "pull", "/sdcard/screenshot.png", local_path])

    # 打开截图文件
    image = Image.open(local_path)

    # 在图像上添加文本水印
    draw = ImageDraw.Draw(image)
    font = ImageFont.truetype("arial.ttf", 32)  # 指定字体和字号
    text = f"error_time: {current_time}"
    draw.text((15, 15), text, fill=(255, 0, 0), font=font)  # 设置文本位置、颜色和字体

    # 保存修改后的截图文件
    image.save(local_path)

    return 0
